/*=====================================================================*
 *                   Copyright (C) 2011 Paul Mineiro                   *
 * All rights reserved.                                                *
 *                                                                     *
 * Redistribution and use in source and binary forms, with             *
 * or without modification, are permitted provided that the            *
 * following conditions are met:                                       *
 *                                                                     *
 *     * Redistributions of source code must retain the                *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer.                                       *
 *                                                                     *
 *     * Redistributions in binary form must reproduce the             *
 *     above copyright notice, this list of conditions and             *
 *     the following disclaimer in the documentation and/or            *
 *     other materials provided with the distribution.                 *
 *                                                                     *
 *     * Neither the name of Paul Mineiro nor the names                *
 *     of other contributors may be used to endorse or promote         *
 *     products derived from this software without specific            *
 *     prior written permission.                                       *
 *                                                                     *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND              *
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,         *
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES               *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE             *
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER               *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,                 *
 * INCIDENTAL, SPECIAL, EXDATAPLARY, OR CONSEQUENTIAL DAMAGES            *
 * (INCLUDING, BUT NOT LIMITED TO, PROCURDATAENT OF SUBSTITUTE           *
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR                *
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF          *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY              *
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE             *
 * POSSIBILITY OF SUCH DAMAGE.                                         *
 *                                                                     *
 * Contact: Paul Mineiro <paul@mineiro.com>                            *
 *=====================================================================*/

#ifndef __MULTIPARSE_H__
#define __MULTIPARSE_H__

#include "binomial.h"
#include "parse.h"

static bool
space_comma_null (char c)
{
  return c == '\0' || isspace (c) || c == ',';
}

static int
parse_label (char**             p,
             unsigned int       n_raw_labels,
             unsigned int       max_raw_labels,
             uint64_t           lineno)
{
  if (**p == '-')
    {
      ++*p;
      return 0;
    }
  else
    {
      unsigned int labels[max_raw_labels];
      unsigned int n_labels = 0;

      while (isdigit (**p) && n_labels < max_raw_labels)
        {
          int val = parse_unsigned_int (p,
                                        n_raw_labels,
                                        space_comma_null,
                                        lineno);

          if (val < 0)
            {
              return -1;
            }

          labels[n_labels] = val;
          ++n_labels;

          if (**p == ',')
            {
              ++*p;
            }
        }

      return set_to_index (labels, n_labels, n_raw_labels);
    }
}

/* clamp? tag| worker:label* */

static int
parse_multi_ratings (FILE*            in,
                     Rating*          ratings,
                     unsigned int     max_ratings,
                     char*            tag,
                     unsigned int     max_tag,
                     int*             clamp,
                     uint64_t         lineno,
                     unsigned int     n_raw_labels,
                     unsigned int     max_raw_labels,
                     unsigned int     n_workers)
{
  char buf[65536];

  if (fgets (buf, sizeof (buf), in) == NULL)
    {
      debug_fprintf (stderr, 
                     "error: can't read buf at input line %llu: %s\n",
                     (unsigned long long) lineno,
                     strerror (errno));

      return -1;
    }

  char* bufptr = buf;

  if (isspace (*bufptr))
    {
      *clamp = -1;
    }
  else
    {
      *clamp = parse_label (&bufptr, n_raw_labels, max_raw_labels, lineno);

      if (*clamp == -1)
        {
          return -1;
        }
    }

  eat_white (&bufptr);
  char* pipe = strchr (bufptr, '|');

  if (pipe == NULL)
    {
      return -1;
    }

  *pipe = '\0';
  strncpy (tag, bufptr, max_tag);
  tag[max_tag - 1] = '\0';

  bufptr = pipe + 1;

  unsigned int r;
  for (eat_white (&bufptr), r = 0;
       *bufptr != '\0' && r < max_ratings;
       eat_white (&bufptr), ++r)
    {
      char* worker = bufptr;

      eat_nonwhite (&bufptr);
      if (*bufptr != '\0') { *bufptr++ = '\0'; }

      char* colon = strrchr (worker, ':');

      if (! colon)
        {
          debug_fprintf (stderr, 
                         "error: no label for worker '%s' at input line %llu\n",
                         worker,
                         (unsigned long long) lineno);

          return -1;
        }

      *colon++ = '\0';
      int label = parse_label (&colon, n_raw_labels, max_raw_labels, lineno);

      if (label < 0)
        {
          return -1;
        }

      ratings[r].worker = hash (worker) % n_workers;
      ratings[r].rating = label;
    }

  if (*bufptr != '\0')
    {
      debug_fprintf (stderr,
                     "error: too many ratings at input line %llu\n",
                     (unsigned long long) lineno);

      return -1;
    }

  return r;
}

#endif /* __MULTIPARSE_H__ */
